package com.yanqu.road.server.channelserver.cmd.xrank;

import com.yanqu.road.entity.enums.rank.eRankInfo;
import com.yanqu.road.entity.enums.rank.eRankRangeType;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.cross3.activity.XRankProto;
import com.yanqu.road.server.channelserver.cmd.ChannelCommand;
import com.yanqu.road.server.channelserver.logic.ChannelMessageHelper;
import com.yanqu.road.server.channelserver.manager.xrank.ChannelXRankMgr;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.server.protocol.Cross3Protocol;

@Cmd(code = ChannelProtocol.N_XRANK_GET_REWARD, desc = "获取排名发奖")
public class XRankGetRewardFromCrossCmd extends ChannelCommand {

    private int activityId;
    private int conditionType;
    private int activityType;
    private int childType;
    private int channelId;
    private long serverId;
    private long userId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        XRankProto.CrossGetXRankRewardReqMsg reqMsg = XRankProto.CrossGetXRankRewardReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        conditionType = reqMsg.getConditionType();
        activityType = reqMsg.getActivityType();
        childType = reqMsg.getChildType();
        channelId = reqMsg.getChannelId();
        serverId = reqMsg.getServerId();
        userId = reqMsg.getUserId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        eRankInfo rankInfo = eRankInfo.getERankInfo(activityType, childType, conditionType);
        if (rankInfo == null) {
            log.error("传过来的类型有问题 或者 没添加枚举类！");
            return;
        }

        int myRank = -1;
        if (rankInfo.getRangeType() == eRankRangeType.User) {
            myRank = ChannelXRankMgr.getUserRankMap().getUserRankListModel(activityId, conditionType).getMyRank(userId);
        } else if (rankInfo.getRangeType() == eRankRangeType.Channel) {
            myRank = ChannelXRankMgr.getChannelRankMap().getChannelRankListModel(activityId, conditionType).getMyRank(channelId);
        }

        XRankProto.GetXRankRewardFromCrossRespMsg.Builder builder = XRankProto.GetXRankRewardFromCrossRespMsg.newBuilder();
        builder.setActivityId(activityId);
        builder.setConditionType(conditionType);
        builder.setMyRank(myRank);
        builder.setServerId(serverId);
        builder.setUserId(userId);
        YanQuMessage message = YanQuMessageUtils.buildMessage(Cross3Protocol.C3_XRANK_GET_REWARD_FROM_CHANNEL, builder);
        ChannelMessageHelper.sendToChannel(message, channelId);
    }

    @Override
    public void sendData() {

    }
}
